<?php
/**
 * Created by JetBrains NetBeans.
 * User: yinyancai
 * Date: 13-3-14 下午4:44
 * To change this template use File | Settings | File Templates.
 */
if (!defined('IN_MOBILE_API')) {
    exit('Access Denied');
}
include_once 'forum.php';
class cloud_api {
    function common() {
    }

    function output() {
        global $_G;
        //判断是否缺少参数
        if (empty($_GET['cpid']) || empty($_GET['cpsid']) || empty($_GET['cuid']) || empty($_GET['cadid']) ||
            empty($_GET['pnumber']) || !is_numeric($_GET['pnumber'])
        ) {
            self::returnMessage(1001);
        }
        $product = C::t('#cloud#cloud_product')->fetch_info($_GET['cpid']);
        $product_standard = C::t('#cloud#cloud_product_standard')->fetch_info($_GET['cpsid']);
        $users = C::t('#cloud#cloud_users')->fetch_info($_GET['cuid']);
        if (empty($product) || empty($product_standard) || empty($users)) {
            self::returnMessage(13000);
        }
        $standard = C::t('#cloud#cloud_standard')->fetch_info($product_standard['csid']);
        if ($product['isviewbate']) {
            $price = $product_standard['reprice'];
            $countPrice = doubleval($product_standard['reprice']) * intval($_GET['pnumber']);
        }
        else {
            $price = $product_standard['price'];
            $countPrice = doubleval($product_standard['price']) * intval($_GET['pnumber']);
        }
        $countArray = C::t('#cloud#cloud_shopping_cart')->find_car_by_users_product_standard_area($_GET['cuid'], $_GET['cpid'], $_GET['cpsid'], $_GET['cadid']);
        if (empty($countArray)) {
            //新增
            $data = array(
                'cuid' => $users['cuid'],
                'cpid' => $product['cpid'],
                'cadid' => $_GET['cadid'],
                'cpsid' => $product_standard['cpsid'],
                'pname' => $product['name'],
                'picurl' => $product['picurl'],
                'sname' => $standard['name'],
                'price' => $price,
                'countprice' => $countPrice,
                'pnumber' => $_GET['pnumber'],
                'status' => 1
            );
            $caid = C::t('#cloud#cloud_shopping_cart')->insert($data);
        }
        else {
            $data = array(
                'countprice' => doubleval($countArray['countprice']) + doubleval($countPrice),
                'pnumber' => intval($countArray['pnumber']) + intval($_GET['pnumber']),
            );
            $caid = C::t('#cloud#cloud_shopping_cart')->update($countArray['cscid'], $data);
        }

        //如果插入失败，这返回错误信息
        if (empty($caid)) {
            self::returnMessage(13000);
        }

        //正常返回
        $variable = array('messagestr' => lang('plugin/cloud', '13001'),);
        cloud_core::result(cloud_core::variable($variable));
    }

    function returnMessage($code) {
        $variable = array(
            'code' => $code,
            'messagestr' => lang('plugin/cloud', '' . $code),
        );
        cloud_core::result(cloud_core::variable($variable));
    }

}

?>

